﻿var ue = UE.getEditor('content', {
    toolbars: [
        ['fullscreen', 'undo', 'redo', 'bold', 'emotion',
            'selectall', 'link', 'insertcode', 'fontfamily', 'fontsize']],
    enableAutoSave: false,
    maximumWords: 500,
    autoHeightEnabled: false,
    autoFloatEnabled: false,
    fontsize: UeditorFontSizeArray
});

var page = 1;

function replyComment(id, name) {
    var top = $('#dvSendComment').offset().top;
    $(window).scrollTop(top - 175);
    if (id > 0) {
        $('#hID').val(id);
        ue.setContent('@' + name + "：")
    }
    ue.focus(true);
}

function gotoComment() {
    var h = $('#dcomments').offset().top - 75;
    $(window).scrollTop(h);
}

function flodReply(o) {
    if ($(o).text() == '折叠') {
        $(o).text('展开');
        $(o).parent().parent().nextAll('div').hide(500);
    } else {
        $(o).text('折叠');
        $(o).parent().parent().nextAll('div').show(500);
    }
}

function delRpComment(id) {
    layer.confirm('确定删除这条回复吗？', {
        icon: 3,
        title: '提示',
        btn: ['确定', '不要'] //按钮
    }, function () {
        $.post(blog_username + "blog/DelRpComment", { id: id }, function (json) {
            if (json.State == 1) {
                layer.msg('删除成功！', { icon: 1 });
                loadComment(page);
            } else {
                layerAlert(json.Message);
            }
        }, "json");
    });
}

function delComment(id) {
    layer.confirm('确定删除这条评论吗？', {
        icon: 3,
        title: '提示',
        btn: ['确定', '不要'] //按钮
    }, function () {
        $.post(blog_username + "blog/DelComment", { id: id }, function (json) {
            if (json.State == 1) {
                layer.msg('删除成功！', { icon: 1 });
                loadComment(page);
                changeCount(-1);
            } else {
                layerAlert(json.Message);
            }
        }, "json");
    });
}

function loadComment(page) {
    $('#dvCommentMain').load(blog_username + "blog/_CommentManager", { aid: articleID, page: page }, function () { doUeParse(); });
}

function changeCount(count) {
    $('#spcount1').html(parseInt($('#spcount1').html()) + count);
    $('#spcount2').html(parseInt($('#spcount2').html()) + count);
}

function showmore(o) {
    var index = layer.load();
    var h = $(o).offset().top;
    var curpage = page = parseInt($("#hidpage").val()) + 1;
    $.post(blog_username + "blog/_CommentManager", { aid: articleID, page: curpage }, function (html) {
        layerMsg("加载成功~", null, null, 1000);
        $(o).parent().remove();
        $('#dvCommentMain').append(html);
        $("#hidpage").val(curpage);
        layer.close(index);
        $("html,body").animate({ scrollTop: h }, 1000);
        doUeParse();
    }, "html");
}

$(function () {
    $('#btnok').click(function () {
        if (!layerCheckLogin(true)) {
            return;
        }
        if (!ue.hasContents()) {
            layerAlert($("#hID").val() == "0" ? '评论内容还没写哦！' : '回复内容还没写哦！', function () { ue.focus(); });
            return;
        }
        var url;
        var prm;
        if ($("#hID").val() == "0") {
            url = blog_username + "blog/SendComment";
            prm = { aid: articleID, pid: 0, content: $.htmlEncode(ue.getContent()) };
        } else {
            url = blog_username + "blog/ReplyComment";
            prm = { pid: $("#hID").val(), aid: articleID, content: $.htmlEncode(ue.getContent()) };
        }
        $.post(url, prm, function (json) {
            if (json.State == 1) {
                layer.msg($("#hID").val() == "0" ? '评论成功！' : '回复成功！', { icon: 1 });
                ue.setContent('');
                $("#hID").val(0);
                $(window).scrollTop(0);
                loadComment(page);
                if ($("#hID").val() == "0") {
                    changeCount(1);
                }
                gotoComment();
            } else {
                layerAlert('操作失败：' + json.Message);
            }
        }, "json");
    });

});

function agree(id, count, a) {
    doAgree(blog_username + 'blog/AgreeArticle', id, count, a);
}

function agreeComment(id, count, a) {
    doAgree(blog_username + 'blog/AgreeComment', id, count, a);
}

function delArticle(id) {
    layerConfirm("确定删除这篇文章吗？", function () {
        $.get(blog_username + "blog/DeleteArticle/" + id, function (s) {
            if (s.State == 1) {
                layerMsg('删除成功！', function () { location.href = blog_username + "blog/Index"; }, null, 1000);
            } else {
                layerAlert(s.Message);
            }
        }, "json");
    });
}

function cai() {
    layerConfirm('你忍心这样踩我么(┬＿┬)？', function () {
        layerConfirm('真的要踩么(┬＿┬)？', function () {
            layerAlert('好吧，我承认这个功能还没做o(∩_∩)o 哈哈');
        });
    });
}